﻿using System.Web.Mvc;
using System.Web.Routing;
using QDT.Common;
using QDT.Common.Config;
using QDT.Common.Data;
using QDT.Common.Events;
using QDT.Common.Plugins;
using QDT.Core.MSData;
using QDT.MVC;
using QDT.Payment;
using QDT.Payment.AllInPay;
using QDT.Services.ArticleMoudule;
using QDT.Services.Assignments;
using QDT.Services.Credits;
using QDT.Services.Deals;
using QDT.Services.Experience;
using QDT.Services.Members;
using QDT.Services.Orders;
using QDT.Services.Projects;
using QDT.Services.SmMailModule;
using log4net;

namespace QDT.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : BaseMvcApplication
    {
        public override void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Guarantee", "Guarantee", new {controller = "Article", action = "Guarantee"});

            routes.MapRoute("HelpCenter", "helpcenter", new { controller = "Article", action = "HelpCenter" });

            routes.MapRoute("Notice", "notice/id-{id}",
                            new { controller = " Article", action = "ShowDetail", id = UrlParameter.Optional });

            routes.MapRoute("Help", "help/id-{id}",
                            new {controller = "Help", action = "Index", id = UrlParameter.Optional});

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        public void Application_BeginRequest()
        {
            var strUrl = Request.Url.ToString().Trim().ToLower();
            if (strUrl.Contains("qiandt.com"))
            {
                Response.RedirectPermanent(strUrl.Replace("qiandt.com", "qiandw.com"));
            }
        }

        public override void RegisterSrevice(IContainerAdapter container)
        {
            container.RegisterAsPerRequest<IUnitOfWork>(x => new DbProvider("QdtDB"));


            //注册Provider
            container.RegisterAsPerRequest<IConfigProvider>(x => new SqlSettingsProvider("QdtDB"));

            container.RegisterAsSingleton<IProjectService, ProjectService>()
                .RegisterAsSingleton<IArticleService,ArticleService>()
                .RegisterAsSingleton<IUserService,UserService>()
                .RegisterAsSingleton<IDealService,DealService>()
                .RegisterAsSingleton<IFormsAuthenticationService,DefaultFormsAuthentication>()
                .RegisterAsSingleton<IQureyService, QureyService>()
                .RegisterAsSingleton<IOrderService,OrderService>()
                .RegisterAsSingleton<IMsgService, MsgService>()
                .RegisterAsSingleton<IAssignmentService, AssignmentService>()
                .RegisterAsSingleton<ICreditService, CreditService>()
                .RegisterAsSingleton<ValidateImage, ValidateImage>()
                .RegisterAsSingleton<IExpService, ExpService>()
                .RegisterAsSingleton<IPayment, AllInPayPayment>();

            EventConfig.Configure(config =>
            {
                config.UsingUnitOfWorkFactory(container.Resolve<IUnitOfWork>);
                config.UsingDefaultEventDispatcher(typeof(Core.Entity).Assembly);
            });

            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));
            Log.SetLog(LogManager.GetLogger("Loger"));
        }



    }
}